﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SharedLib.Engine;
using Lidgren.Network;

namespace SharedLib.Network.Server
{
    public class ServerChunk
    {
        public List<Entity> Entities;
        public byte[,] Tiles;
        public byte[,] Collisions;
        public Int32 X;
        public Int32 Y;

        public ServerChunk()
        {
            X = 0;
            Y = 0;
            Entities = new List<Entity>();
            Tiles = new byte[Constants.CHUNK_WIDTH, Constants.CHUNK_HEIGHT];
            Collisions = new byte[Constants.CHUNK_WIDTH, Constants.CHUNK_HEIGHT];
        }

        public void Update(long time)
        {
            for (int i = Entities.Count-1; i > -1 ; i--)
            {
                Entities[i].Update(time);
            }

            List<Entity> deletedPlayers = Entities.Where(p => p.GetType() == typeof(ServerPlayer) && ((ServerPlayer)p).NetConn.Status == NetConnectionStatus.Disconnected).ToList();
            foreach (var item in deletedPlayers)
            {
                Entities.Remove(item);
                Server.Instance.ServerSender.SendEntityLeft(item);
                Server.Instance.EntitiesManager.EntitiesList.Remove(item.ID);
            }
        }
    }
}
